package com.twu.biblioteca;

import java.util.*;
import java.io.*;

public class BibliotecaApp
{
    static ManageFunctionalities m=new ManageFunctionalities();
    static Scanner s=new Scanner(System.in);
    static int option;
    static String yORn="y";
    static String yORnLogin="y";
    static boolean IsAuthentic=false;

    public static void main(String[] args)
    {
        System.out.println("Welcome to The Bangalore Public Library");
        ShowOptionsWhichDoNotRequireLogin();
        System.out.println("Do you want to login(y/n)?");
        yORnLogin=s.next();
        if(yORnLogin.equalsIgnoreCase("y"))
        {
            checklogin();
            while(IsAuthentic==false)
            {
                System.out.println("User Library number or password incorrect\nPlease try again");
                checklogin();
            }
            ShowOptionsWhichRequireLogin();
            SelectOptionWithLogin();
        }
        else SelectOptionWithoutLogin();
    }

    public static String username="111-1111";
    public static String password="patankar30";
    private static void checklogin()
    {
        System.out.println("User Library number:");
        username=s.next();
        System.out.println("Password:");
        password=s.next();
        IsAuthentic=m.checkAuthentication(username,password);

    }

    public static void SelectOptionWithoutLogin()
    {
        System.out.println("Select an option (Enter 1 or 2):");
        option=getOption();
        try
        {
            if(option!=1&&option!=2)
            {
                NotValidOptionWithoutLogin();
            }
            else if(option==1)
            {
                m.ViewListOfBooks();
                ContinueAppWithoutLogin();
            }
            else if(option==2)
            {
                m.ViewListOfMovies();
                ContinueAppWithoutLogin();
            }
        }
        catch(Exception e)
        {
            NotValidOptionWithoutLogin();
        }
    }
    public static void SelectOptionWithLogin()
    {
        System.out.println("Select an option (Enter 3 or 4):");
        option=getOption();
        try
        {
            if(option!=3&&option!=4)
            {
                NotValidOptionWithLogin();
            }
            else if(option==3)
            {
                m.ReserveBook();
                ContinueAppWithLogin();
            }
            else if(option==4)
            {
                System.out.println("Please talk to a Librarian. Thank you");
                ContinueAppWithLogin();
            }
        }
        catch(Exception e)
        {
            NotValidOptionWithLogin();
        }
    }

    public static int getOption()
    {
        int opt=s.nextInt();
        return opt;
    }

    public static void ShowOptionsWhichDoNotRequireLogin()
    {
        System.out.println("We are providing following facilities:");
        System.out.println("1 View List of Books");
        System.out.println("2 View List of Movies");
    }
    public static void ShowOptionsWhichRequireLogin()
    {
        System.out.println("We are providing following facilities:");
        System.out.println("3 Reserve a book for collection");
        System.out.println("4 Check your membership details");
    }
    public static void NotValidOptionWithoutLogin()
    {
        System.out.println("Select a valid option!!");
        SelectOptionWithoutLogin();
    }
    public static void NotValidOptionWithLogin()
    {
        System.out.println("Select a valid option!!");
        SelectOptionWithLogin();
    }
    public static void ContinueAppWithoutLogin()
    {
        System.out.println("Do you want to continue(y/n)?");
        yORn=s.next();
        if(yORn.equalsIgnoreCase("y"))
        {
            System.out.println("Do you want to Login(y/n)?");
            yORnLogin=s.next();
            if(yORnLogin.equalsIgnoreCase("y"))
            {
                checklogin();
                while(IsAuthentic==false)
                {
                    System.out.println("User Library number or password incorrect\nPlease try again");
                    checklogin();
                }
                ShowOptionsWhichRequireLogin();
                SelectOptionWithLogin();
            }
            else
            {
                ShowOptionsWhichDoNotRequireLogin();
                SelectOptionWithoutLogin();
            }
        }
        else if(yORn.equalsIgnoreCase("n"))
            System.out.println("Please Visit again");
    }
    public static void ContinueAppWithLogin()
    {
        System.out.println("Do you want to continue(y/n)?");
        yORn=s.next();
        if(yORn.equalsIgnoreCase("y"))
        {
            ShowOptionsWhichRequireLogin();
            SelectOptionWithLogin();
        }
        else if(yORn.equalsIgnoreCase("n"))
            System.out.println("Please Visit again");
        else
            ContinueAppWithLogin();
    }
    public String getContinuityResponse()
    {
        String con=yORn;
         return con;
    }
    public int takeCustomerResponse()
    {
        int res=s.nextInt();
        return res;
    }
    public String TestloginUser()
    {
        String user=username;
        return user;

    }
    public String TestloginPass()
    {
    String pass=password;
        return pass;
    }

}